package com.blog.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.base.controller.BaseController;
import com.blog.base.validator.InspectorException;
import com.blog.web.bean.Article;
import com.blog.web.service.ArticleService;

/**
 * @todo articleController
 * @author fg
 * @date 2016年3月24日
 */
@Scope("prototype")
@Controller
@RequestMapping(value="/article")
public class ArticleController extends BaseController
{
	@Resource
	ArticleService articleService;
	/**
	 * @todo 条件查询文章
	 * @author fg
	 * @date 2016年3月24日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("queryAllArticle")
	public ModelAndView queryarticleByOption(HttpServletRequest request) 
	{
		List<Article> listArticle = articleService.queryArticleByOption(getRequestMap());
		mav.addObject("articleList", listArticle);
		mav.setViewName("admin/article/article_list");
		return mav;
	}
	
	/**
	 * @todo 打开添加文章之前
	 * @author fg
	 * @date 2016年3月26日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping(value="/preAddArticle")
	public ModelAndView preAddarticle(HttpServletRequest request) 
	{
		mav.setViewName("admin/article/article_add");
		return mav;
	}
	
	/**
	 * @todo 打开添加文章之前
	 * @author fg
	 * @date 2016年4月3日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/preEditarticle")
	public ModelAndView preEditarticle(Integer id) 
	{
		mav.addObject("article",articleService.queryArticleById(id));
		mav.setViewName("admin/article/article_add");
		return mav;
	}
	
	/**
	 * @todo 保存/新增文章
	 * @author fg
	 * @throws InspectorException 
	 * @date 2016年4月3日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/addOrSaveArticle")
	@ResponseBody
	public JSONObject addarticle(@Valid Article article,BindingResult result) throws InspectorException 
	{
		if(result.hasErrors())
			throwInspectorException(result);
		try
		{
			Integer success =  articleService.addOrSaveArticle(article);
			json.put("msg", "保存成功！");
			json.put("success", success);
		} 
		catch (Exception e)
		{
			json.put("msg", e.toString());
			json.put("success", 0);
		}
		
		return json;
	}
	
	
	/**
	 * @todo 隐藏以及删除操作
	 * @author fg
	 * @date 2016年4月3日
	 */
	@RequiresRoles("superAdmin")
	@RequestMapping("/hideOrRemoveArtlcle")
	@ResponseBody
	public JSONObject hideOrRemoveArtlcle(Integer id,Integer type,Integer action) 
	{
		try
		{
			Article article = new Article();
			article.setId(id);
			if(new Integer(1).equals(type))
			{//hide
				article.setVisible(action);
			}
			else
			{
				article.setIsDeleted(action);
			}
			Integer success =  articleService.modifyArticle(article);
			json.put("msg", "操作成功！");
			json.put("success", success);
		} 
		catch (Exception e)
		{
			json.put("msg", e.toString());
			json.put("success", 0);
		}
		
		return json;
	}
}
